<?php
class MenuTree
{
    var $security_role;
    var $menu_users = "{text: 'User Manajement',
                                id: 'jun.UsersGrid',
                                leaf: true
                                },";
    var $security = "{text: 'Security Roles',
                                id: 'jun.SecurityRolesGrid',
                                leaf: true
                                },";
    function __construct($id)
    {
        $role = SecurityRoles::model()->findByPk($id);
        $this->security_role = explode(",", $role->sections);
    }
    function getChildMaster()
    {
        $child = "";
        $child .= in_array(100, $this->security_role) ? "{
                            text: 'Country',
                            id: 'jun.NegaraGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(100, $this->security_role) ? "{
                            text: 'Province',
                            id: 'jun.ProvinsiGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(100, $this->security_role) ? "{
                            text: 'City',
                            id: 'jun.KotaGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(100, $this->security_role) ? "{
                            text: 'Sub District',
                            id: 'jun.KecamatanGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(100, $this->security_role) ? "{
                            text: 'Bank',
                            id: 'jun.BankGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(101, $this->security_role) ? "{
                            text: 'Group',
                            id: 'jun.GrupGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(102, $this->security_role) ? "{
                            text: 'Items',
                            id: 'jun.BarangGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(103, $this->security_role) ? "{
                            text: 'Customers',
                            id: 'jun.CustomersGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(104, $this->security_role) ? "{
                            text: 'Beautician',
                            id: 'jun.BeautyGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(105, $this->security_role) ? "{
                            text: 'Beauty Tips Class',
                            id: 'jun.GolGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(106, $this->security_role) ? "{
                            text: 'Value Tips Class',
                            id: 'jun.PriceGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(107, $this->security_role) ? "{
                            text: 'Reference',
                            id: 'jun.SysTypesGrid',
                            leaf: true
                        }" : '';
        return $child;
    }
    function getMaster($child)
    {
        if ($child == null)
            return "";
        return "{
                    text: 'Master',
                    expanded: false,
                    children:[
                    $child
                    ]
                },";
    }
    function getTransaction($child)
    {
        if ($child == null)
            return "";
        return "{
                    text: 'Transaction',
                    expanded: false,
                    children:[
                    $child
                    ]
                },";
    }
    function getChildTransaction()
    {
        $child = "";
        $child .= in_array(200, $this->security_role) ? "{
                            text: 'Sales',
                            id: 'jun.SalestransGrid',
                            leaf: true
                        }," : '';

        $child .= in_array(203, $this->security_role) ? "{
                            text: 'Return Sales',
                            id: 'jun.ReturSalestransgrid',
                            leaf: true
                    }," : '';
        $child .= in_array(201, $this->security_role) ? "{
                            text: 'Service',
                            id: 'jun.beautytransGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(202, $this->security_role) ? "{
                            text: 'Cash In',
                            id: 'jun.KasGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(203, $this->security_role) ? "{
                            text: 'Cash Out',
                            id: 'jun.KasGridOut',
                            leaf: true
                    }," : '';
        $child .= in_array(203, $this->security_role) ? "{
                            text: 'Receive Supplier Item',
                            id: 'jun.TransferItemGrid',
                            leaf: true
                    }," : '';
        $child .= in_array(203, $this->security_role) ? "{
                            text: 'Return Supplier Item',
                            id: 'jun.ReturnTransferItemGrid',
                            leaf: true
                    }," : '';
        $child .= in_array(203, $this->security_role) ? "{
                            text: 'Tender Declaration',
                            id: 'jun.TenderGrid',
                            leaf: true
                    }," : '';
        $child .= in_array(203, $this->security_role) ? "{
                            text: 'Audit',
                            id: 'jun.AuditGrid',
                            leaf: true
                    }," : '';
        return $child;
    }
    function getReport($child)
    {
        if ($child == null)
            return "";
        return "{
                    text: 'Report',
                    expanded: false,
                    children:[
                    $child
                    ]
                },";
    }
    function getChildReport()
    {
        $child = "";
        $child .= in_array(300, $this->security_role) ? "{
                            text: 'Sales Summary',
                            id: 'jun.ReportSalesSummary',
                            leaf: true
                        }," : '';
        $child .= in_array(301, $this->security_role) ? "{
                            text: 'Sales Details',
                            id: 'jun.ReportSalesDetails',
                            leaf: true
                        }," : '';
        $child .= in_array(302, $this->security_role) ? "{
                            text: 'Inventory Movements',
                            id: 'jun.ReportInventoryMovements',
                            leaf: true
                        }," : '';
        $child .= in_array(303, $this->security_role) ? "{
                            text: 'Inventory Card',
                            id: 'jun.ReportInventoryCard',
                            leaf: true
                    }," : '';
        $child .= in_array(304, $this->security_role) ? "{
                            text: 'Beautician Services Summary',
                            id: 'jun.ReportBeautySummary',
                            leaf: true
                        }," : '';
        $child .= in_array(305, $this->security_role) ? "{
                            text: 'Beautician Services Details',
                            id: 'jun.ReportBeautyDetails',
                            leaf: true
                        }," : '';
        $child .= in_array(306, $this->security_role) ? "{
                            text: 'New Customers',
                            id: 'jun.ReportNewCustomers',
                            leaf: true
                        }," : '';
        $child .= in_array(307, $this->security_role) ? "{
                            text: 'Tenders',
                            id: 'jun.beautytransGrid',
                            leaf: true
                    }," : '';
        $child .= in_array(308, $this->security_role) ? "{
                            text: 'Cash Out',
                            id: 'jun.ReportBiaya',
                            leaf: true
                    }," : '';
        return $child;
    }
    function getAdministration($child)
    {
        if ($child == null)
            return "";
        return "{
                    text: 'Administration',
                    expanded: false,
                    children:[
                    $child
                    ]
                },";
    }
    function getChildAdministration()
    {
        $child = "";
        $child .= in_array(400, $this->security_role) ? "{
                            text: 'User Manajement',
                            id: 'jun.UsersGrid',
                            leaf: true
                        }," : '';
        $child .= in_array(401, $this->security_role) ? "{
                            text: 'Security Roles',
                            id: 'jun.SecurityRolesGrid',
                            leaf: true
                        }," : '';
        return $child;
    }
    function getGeneral()
    {
        $username = Yii::app()->user->name;
        $child = "";
        $child .= in_array(001, $this->security_role) ? "{
                            text: 'Change Password',
                            id: 'jun.PasswordWin',
                            leaf: true
                        }," : '';
        $child .= in_array(002, $this->security_role) ? "{
                            text: 'Logout ($username)',
                            id: 'logout',
                            leaf: true
                        }," : '';
        return $child;
    }
    public function get_menu()
    {
        $username = Yii::app()->user->name;
        $data = "[";
        $data .= self::getMaster(self::getChildMaster());
        $data .= self::getTransaction(self::getChildTransaction());
        $data .= self::getReport(self::getChildReport());
        $data .= self::getAdministration(self::getChildAdministration());
        $data .= self::getGeneral();
        $data .= "]";
        return $data;
    }
}
